<!DOCTYPE html>
<html>

<head>
  <title>Weekly voca</title>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
  <script src="https://unpkg.com/axios/dist/axios.min.js"></script>

  <style>
    body {
      font-family: Menlo, Consolas, monospace;
      color: azure;
      background-color: #666;
    }

    .item {
      cursor: pointer;
    }

    .bold {
      font-weight: bold;
    }

    .footer {
      border-radius:10px;
      margin: 0 auto;
      width: 80%;
      padding: 1em;
      background: #a5a5a5aa;
      box-shadow: 0 4px 8px 0 #eeeeeedd, 0 6px 20px 0 rgba(0, 0, 0, 0.19);
      text-align: center;
    }

    ul {
      padding-left: 1em;
      line-height: 1.5em;
      list-style-type: none;
      background-color: #444;
      border-style: ridge;
    }

    textarea {
      width: 100%;
      height: 6em;
      background-color: #444;
      color: azure
    }

    a {
      color: #c7edff;
      background-color: #66ccff;
      border: none;
      border-radius:25px;
      padding: 15px 32px;
      text-align: center;
      text-decoration: none;
      display: inline-block;
      font-size: 16px;
    }
  </style>
</head>

<body>
  <script type="text/x-template" id="item-template">
        <li>
            <div :class="{bold: isFolder}" @click="toggle">
            <p>{{ index }}<span v-if="isFolder">[{{ isOpen ? '-' : '+' }}]</span></p>
            </div>
            <ul  v-show="isOpen" v-if="isFolder" v-for="(child,childIndex) in item">
                <tree-item class="item" v-if="child instanceof Object" :key="String(childIndex)" :item="child" :index="childIndex"></tree-item>
                <p v-else>{{childIndex}}: {{child}}</p>
            </ul>
            <p v-if="!isFolder">{{item}}</p>
        </li>
    </script>

  <div id="demo">
    <textarea v-model="input"></textarea>
    <ul v-for="(word,index) in treeData">
      <tree-item class="item" :item="word" :index="String(index)"></tree-item>
    </ul>
  </div>

  <div class="footer">
    <p>事实上,从逻辑层面和这个鶸的的能力来看,这个页面没啥可优化的了.</p>
    <p>但如果你<br>不喜欢那个<span class="bold">不常用的JSON输入框一直占着地方</span>,<br>
      喜欢<span class="bold">更优雅的界面</span>,<br>
      并能忍受那个作为测试版的页面某些时候<span class="bold">因为更新崩掉</span></p>
    <p><span class="bold">↓ click here ↓</span> </p>
    <a href="test.html">极其不稳定但会随着这个鶸能力逐渐提高而逐渐cool起来的测试版</a>
  </div>

  <script>
    "use strict"

    Vue.component('tree-item', {
      template: '#item-template',
      props: {
        index: String,
        item: Object
      },
      data: function () {
        return {
          isOpen: false
        }
      },
      computed: {
        isFolder: function () {
          return this.item instanceof Object
        }
      },
      methods: {
        toggle: function () {
          if (this.isFolder) {
            this.isOpen = !this.isOpen
          }
        }
      }
    })


    const demo = new Vue({
      el: '#demo',
      data: {
        input: '{"status":"loading"}',
        treeData: Object
      },
      created: function () {
        axios.get("words_in_spoc.json")
          .then(response => {
            this.treeData = response.data;
            this.input = JSON.stringify(this.treeData);
          });

      },
      watch: {
        input: function (newInput, oldInput) {
          this.treeData = JSON.parse(String(newInput));
        }
      },
    });

  </script>

</body>